package com.xmtlxx.web.controller.tms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.web.controller.center.service.ICommonService;
import com.xmtlxx.web.controller.tms.service.IWaybillService;
import com.xmtlxx.tms.domain.Order;
import com.xmtlxx.tms.domain.Waybill;
import com.xmtlxx.tms.domain.WaybillDtl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.sms.aliyun.AliyunSmsService;
import com.xmtlxx.common.sms.aliyun.SmsRequest;
import com.xmtlxx.common.sms.aliyun.SmsTemplateCode;
import com.xmtlxx.common.sms.aliyun.SmsTemplateParam;
import com.xmtlxx.common.utils.DateUtils;
import com.xmtlxx.common.utils.JsonUtils;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.oms.domain.Task;
import com.xmtlxx.oms.domain.vo.TaskVo;
import com.xmtlxx.oms.mapper.TaskMapper;
import com.xmtlxx.system.domain.SysOss;
import com.xmtlxx.system.domain.bo.SysOssBo;
import com.xmtlxx.system.mapper.SysOssMapper;
import com.xmtlxx.system.mapper.SysUserMapper;
import com.xmtlxx.tms.domain.bo.WaybillBo;
import com.xmtlxx.tms.domain.bo.WaybillDtlBo;
import com.xmtlxx.tms.domain.vo.OrderVo;
import com.xmtlxx.tms.domain.vo.WaybillDtlVo;
import com.xmtlxx.tms.domain.vo.WaybillVo;
import com.xmtlxx.tms.mapper.OrderMapper;
import com.xmtlxx.tms.mapper.WaybillDtlMapper;
import com.xmtlxx.tms.mapper.WaybillMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 运单Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class WaybillServiceImpl implements IWaybillService {

    private final WaybillMapper baseMapper;

    private final WaybillDtlMapper waybillDtlMapper;

    private final AliyunSmsService aliyunSmsService;

    private final OrderMapper orderMapper;


    private final ICommonService commonService;

    private final TaskMapper taskMapper;

    private final SysUserMapper userMapper;

    private final SysOssMapper ossMapper;

    /**
     * 查询运单
     *
     * @param id 运单主键
     * @return 运单
     */
    @Override
    public WaybillVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectWaybillVoByID(id);
    }

    @Override
    public WaybillVo queryByOrderId(String id) {
        return baseMapper.selectWaybillVoByOrderID(id);
    }

    /**
     * 查询运单列表
     *
     * @param bo 运单
     * @return 运单
     */
    @Override
    public TableDataInfo<WaybillVo> queryPageList(WaybillBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<Waybill> lqw = buildQueryWrapper(bo);
       // Page<WaybillVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<WaybillVo> result = baseMapper.selectPageWaybillList(pageQuery.build(), bo);
        if(result != null && result.getRecords() != null && result.getRecords().size() > 0){
            for (WaybillVo vo:result.getRecords()){
//                if(LoginHelper.getLoginUser().getRolePermission().contains("Admin")){
//                    vo.setIsShow(true);
//                } else if(LoginHelper.getLoginUser().getRolePermission().contains("smscg")){
//                    vo.setIsShow(true);
//                }else if (StringUtils.isNotEmpty(vo.getOrderType()) && !vo.getOrderType().equals("0")){
//                    vo.setIsShow(true);
//                } else {
//                    vo.setIsShow(false);
//                }
                OrderVo orderVo = orderMapper.selectOrderVoByID(vo.getOrderId());
                if(orderVo != null){
                    vo.setOrderStatus(orderVo.getStatus());
                    vo.setCarStatus(orderVo.getCarStatus());
                    vo.setSendTel(orderVo.getSendTel());
                    if(orderVo.getStatus().equals("2")){
                        vo.setIsCarShow(true);
                        if(orderVo.getType().equals("0")){
                            vo.setIsShow(true);
                        }
                    }

                    if(orderVo.getBuss().equals("1") && orderVo.getCarStatus().equals("4")){
                        boolean flag = false;
                        SysOssBo orderOssBo = new SysOssBo();

                        orderOssBo.setObjid(orderVo.getId());

                        orderOssBo.setBuss("take");
                        List<SysOss> takeOss = ossMapper.selectSysOssList(orderOssBo);
                        if(takeOss == null || takeOss.size() == 0){
                            flag = true;
                        }

                        orderOssBo.setBuss("unload");
                        List<SysOss> unloadOss = ossMapper.selectSysOssList(orderOssBo);
                        if(unloadOss == null || unloadOss.size() == 0){
                            flag = true;
                        }
                        vo.setIsArrive(flag);
                    }
                }
            }
        }
        return TableDataInfo.build(result);
    }

    @Override
    public String getTaskVideoStatus(String id) {
        return baseMapper.getTaskVideoStatus(id);
    }

    /**
     * 查询运单列表
     *
     * @param bo 运单
     * @return 运单
     */
    @Override
    public List<WaybillVo> queryList(WaybillBo bo) {
       // LambdaQueryWrapper<Waybill> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectWaybillList(bo);
    }

    private LambdaQueryWrapper<Waybill> buildQueryWrapper(WaybillBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Waybill> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), Waybill::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getCarrierId()), Waybill::getCarrierId, bo.getCarrierId());
        lqw.eq(StringUtils.isNotBlank(bo.getScheduleId()), Waybill::getScheduleId, bo.getScheduleId());
        lqw.eq(StringUtils.isNotBlank(bo.getCarId()), Waybill::getCarId, bo.getCarId());
        lqw.eq(StringUtils.isNotBlank(bo.getCarNumber()), Waybill::getCarNumber, bo.getCarNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getDriverId()), Waybill::getDriverId, bo.getDriverId());
        lqw.like(StringUtils.isNotBlank(bo.getDriverName()), Waybill::getDriverName, bo.getDriverName());
        lqw.between(params.get("beginDispatchTime") != null && params.get("endDispatchTime") != null,
            Waybill::getDispatchTime ,params.get("beginDispatchTime"), params.get("endDispatchTime"));
        lqw.between(params.get("beginTakeBillTime") != null && params.get("endTakeBillTime") != null,
            Waybill::getTakeBillTime ,params.get("beginTakeBillTime"), params.get("endTakeBillTime"));
        lqw.between(params.get("beginTakeTime") != null && params.get("endTakeTime") != null,
            Waybill::getTakeTime ,params.get("beginTakeTime"), params.get("endTakeTime"));
        lqw.between(params.get("beginSendTime") != null && params.get("endSendTime") != null,
            Waybill::getSendTime ,params.get("beginSendTime"), params.get("endSendTime"));
        lqw.between(params.get("beginDeliverTime") != null && params.get("endDeliverTime") != null,
            Waybill::getDeliverTime ,params.get("beginDeliverTime"), params.get("endDeliverTime"));
        lqw.between(params.get("beginArriveTime") != null && params.get("endArriveTime") != null,
            Waybill::getArriveTime ,params.get("beginArriveTime"), params.get("endArriveTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Waybill::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getReceiptStatus()), Waybill::getReceiptStatus, bo.getReceiptStatus());
        lqw.eq(bo.getDispatcherId() != null, Waybill::getDispatcherId, bo.getDispatcherId());
        lqw.eq(StringUtils.isNotBlank(bo.getDispatcher()), Waybill::getDispatcher, bo.getDispatcher());
        lqw.eq(StringUtils.isNotBlank(bo.getSigner()), Waybill::getSigner, bo.getSigner());
        lqw.eq(bo.getSignDeptId() != null, Waybill::getSignDeptId, bo.getSignDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), Waybill::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增运单
     *
     * @param bo 运单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public WaybillVo insertByBo(WaybillBo bo) {
        WaybillVo vo = new WaybillVo();
        Waybill add = BeanUtil.toBean(bo, Waybill.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setWaybillNo(getNo());
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;

        Order order = orderMapper.selectById(add.getOrderId());
        Task task = taskMapper.selectById(order.getTaskId());

        List<WaybillDtl> dtls = new ArrayList<>();
        if(bo.getItems() != null && bo.getItems().size() > 0){
            dtls = bo.getItems().stream().map(item->{
                WaybillDtl dtl = BeanUtil.toBean(item, WaybillDtl.class);
                dtl.setId(IdUtil.simpleUUID());
                dtl.setWaybillId(add.getId());

                /**
                 * 物品日志
                 */
                String info = JsonUtils.toJsonString(dtl);
                String logContent = bo.getLogContent() +"：物品编号："+item.getPno()+"，物品名称："+item.getName();
                commonService.savePropertyLog(dtl.getPno(),add.getId(),dtl.getName(),bo.getLogType(),logContent,order.getDeptId(),
                    info,task.getStoreId(),bo.getNums().longValue(),null);


                return dtl;
            }).collect(Collectors.toList());
            waybillDtlMapper.insertBatch(dtls);
        }

        if (flag) {
            bo.setId(add.getId());

            //运单日志
            commonService.saveWaybillLog(bo,"0");//运单创建
            commonService.saveWaybillLog(bo,"1");//派单
//            if(bo.getBuss().equals("1")){//车辆单，新增一条派单日志
//                commonService.saveWaybillLog(bo,"1");//派单
//            }
        }
        BeanUtil.copyProperties(add,vo);

        /**
         * 订单指派通知
         */
        SmsRequest smsRequest = new SmsRequest();
        smsRequest.setTemplateCode(SmsTemplateCode.DISPATCH_NOTICE.getSMS_CODE());
        smsRequest.setPhoneNumber(task.getTel());

        SmsTemplateParam templateParam = new SmsTemplateParam();
        templateParam.setOrderCode(order.getOrderNo());
        if(order.getPickupId() != null){
            templateParam.setName(userMapper.selectUserById(order.getPickupId()).getNickName());
            templateParam.setPhone(userMapper.selectUserById(order.getPickupId()).getPhonenumber());
        }else{
            templateParam.setName(order.getTakeContacts());
            templateParam.setPhone(order.getTakeTel());
        }
        templateParam.setOptType(task.getType().equals("0")?"上门取件":"物流配送");

        smsRequest.setTemplateParam(templateParam);
        aliyunSmsService.AliyunSmsSend(smsRequest);

        /**
         * 上门取件通知
         * 0-物品 1-车辆
         */
        if(order.getBuss().equals("0") && order.getPickupId() != null){
            String orderType = null;

            switch (task.getType()){
                case "0":
                    orderType = "入库";
                    break;
                case "1":
                    orderType = "出库";
                    break;
                case "2":
                    orderType = "处置";
                    break;
                case "3":
                    orderType = "调用";
                    break;

            }
            SmsRequest pSmsRequest = new SmsRequest();
            pSmsRequest.setTemplateCode(SmsTemplateCode.PICK_UP_NOTICE.getSMS_CODE());
            pSmsRequest.setPhoneNumber(task.getTel());

            SmsTemplateParam pTemplateParam = new SmsTemplateParam();
            pTemplateParam.setOrderType(orderType);
            pTemplateParam.setOrderCode(order.getOrderNo());
            pTemplateParam.setName(userMapper.selectUserById(order.getPickupId()).getNickName());
            pTemplateParam.setPhone(userMapper.selectUserById(order.getPickupId()).getPhonenumber());
            pSmsRequest.setTemplateParam(pTemplateParam);
            aliyunSmsService.AliyunSmsSend(pSmsRequest);
        }

        return vo;
    }

    /**
     * 修改运单
     *
     * @param bo 运单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public WaybillVo updateByBo(WaybillBo bo) {
        WaybillVo vo = new WaybillVo();
        Waybill update = BeanUtil.toBean(bo, Waybill.class);
        validEntityBeforeSave(update);
        Integer nums = update.getNums();

        /**
         * 查询原明细数据
         */
        WaybillDtlBo dtlBo = new WaybillDtlBo();
        dtlBo.setWaybillId(update.getId());
        List<WaybillDtlVo> olds = waybillDtlMapper.selectWaybillDtlList(dtlBo);

        List<WaybillDtl> dtls = new ArrayList<>();

        if(bo.getItems() != null && bo.getItems().size() > 0){
            dtls = bo.getItems().stream().map(item->{
                WaybillDtl dtl = new WaybillDtl();
                BeanUtil.copyProperties(item,dtl);
                if(StringUtils.isEmpty(dtl.getId())){//修改后的物品
                    dtl.setId(IdUtil.simpleUUID());
                    dtl.setDelFlag("0");
                    dtl.setStatus("0");
                    dtl.setWaybillId(update.getId());
                }
                /**
                 * 迭代查询移除的明细
                 */
                Iterator iterator = olds.iterator();
                while(iterator.hasNext()){
                    WaybillDtlVo waybillDtl = (WaybillDtlVo) iterator.next();
                    if(waybillDtl.getId().equals(dtl.getId())){
                        iterator.remove();
                    }
                }

                return dtl;
            }).collect(Collectors.toList());

            nums = dtls.stream().mapToInt(item-> item.getNums().intValue()).sum();
        }
        update.setNums(nums);

        if(dtls.size() > 0){
            waybillDtlMapper.insertOrUpdateBatch(dtls);
        }
        //已删除的物品处理：删除dtl表、回退可移交状态
        if(olds != null && olds.size() > 0){
            olds.forEach(item -> {
                waybillDtlMapper.deleteVoById(item.getId());
            });
        }

//        Order order = orderMapper.selectById(update.getOrderId());
//        Task task = taskMapper.selectById(order.getTaskId());

        //更新运单数据

//        List<WaybillDtl> dtls = new ArrayList<>();
//        if(bo.getItems() != null && bo.getItems().size() > 0){
//            dtls = bo.getItems().stream().map(item->{
//                WaybillDtl dtl = waybillDtlMapper.selectOne(new LambdaQueryWrapper<WaybillDtl>().eq(WaybillDtl::getWaybillId,update.getId())
//                    .eq(WaybillDtl::getPno,item.getPno()));
//                if(dtl == null){
//                    dtl.setId(IdUtil.simpleUUID());
//                    dtl.setWaybillId(update.getId());
//                    dtl.setDelFlag("0");
//                }
//
//                /**
//                 * 物品日志
//                 */
//                String info = JsonUtils.toJsonString(dtl);
//                String logContent = bo.getLogContent() +"：物品编号："+item.getPno()+"，物品名称："+item.getName();
//                commonService.savePropertyLog(dtl.getPno(),update.getId(),dtl.getName(),bo.getLogType(),logContent,order.getDeptId(),
//                    info,task.getStoreId(),bo.getNums().longValue(),null);
//
//
//                return dtl;
//            }).collect(Collectors.toList());
//            waybillDtlMapper.insertOrUpdateBatch(dtls);
//        }
//
        boolean flag = baseMapper.updateById(update) > 0;
        if(flag){
            BeanUtil.copyProperties(update,vo);
        }

        return vo;
    }

    @Override
    public WaybillVo updateBySignBo(WaybillBo bo) {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WaybillVo updateWaybillByBo(WaybillBo bo) {
        WaybillVo vo = new WaybillVo();
        OrderVo order = orderMapper.selectOrderVoByID(bo.getOrderId());
        Waybill update = BeanUtil.toBean(bo, Waybill.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if(flag){
            BeanUtil.copyProperties(update,vo);
        }
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WaybillVo dispatchWaybillByBo(WaybillBo bo) {
        WaybillVo vo = new WaybillVo();
        OrderVo order = orderMapper.selectOrderVoByID(bo.getOrderId());
        TaskVo task = taskMapper.selectTaskVoByID(order.getTaskId());
        Waybill update = BeanUtil.toBean(bo, Waybill.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if(flag){
            BeanUtil.copyProperties(update,vo);

            /**
             * 订单指派通知
             */
            SmsRequest smsRequest = new SmsRequest();
            smsRequest.setTemplateCode(SmsTemplateCode.DISPATCH_NOTICE.getSMS_CODE());
            smsRequest.setPhoneNumber(task.getTel());

            SmsTemplateParam templateParam = new SmsTemplateParam();
            templateParam.setOrderCode(order.getOrderNo());
            if(order.getPickupId()!= null){
                templateParam.setName(userMapper.selectUserById(order.getPickupId()).getNickName());
                templateParam.setPhone(userMapper.selectUserById(order.getPickupId()).getPhonenumber());
                templateParam.setOptType(task.getType().equals("0")?"上门取件":"物流配送");
                smsRequest.setTemplateParam(templateParam);
                aliyunSmsService.AliyunSmsSend(smsRequest);
            }


            /**
             * 上门取件通知
             */
            String orderType = null;

            switch (task.getType()){
                case "0":
                    orderType = "入库";
                    break;
                case "1":
                    orderType = "出库";
                    break;
                case "2":
                    orderType = "处置";
                    break;
                case "3":
                    orderType = "调用";
                    break;

            }
            SmsRequest pSmsRequest = new SmsRequest();
            pSmsRequest.setTemplateCode(SmsTemplateCode.PICK_UP_NOTICE.getSMS_CODE());
            pSmsRequest.setPhoneNumber(task.getTel());

            SmsTemplateParam pTemplateParam = new SmsTemplateParam();
            pTemplateParam.setOrderType(orderType);
            pTemplateParam.setOrderCode(order.getOrderNo());
           if(order.getPickupId() != null){
               pTemplateParam.setName(userMapper.selectUserById(order.getPickupId()).getNickName());
               pTemplateParam.setPhone(userMapper.selectUserById(order.getPickupId()).getPhonenumber());
               pSmsRequest.setTemplateParam(pTemplateParam);
               aliyunSmsService.AliyunSmsSend(pSmsRequest);
           }

        }
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WaybillVo resetDispatch(WaybillBo bo) {
        WaybillVo vo = new WaybillVo();
        OrderVo order = orderMapper.selectOrderVoByID(bo.getOrderId());
        TaskVo task = taskMapper.selectTaskVoByID(order.getTaskId());
        Waybill update = BeanUtil.toBean(bo, Waybill.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if(flag){
            BeanUtil.copyProperties(update,vo);

            /**
             * 订单指派通知
             */
            SmsRequest smsRequest = new SmsRequest();
            smsRequest.setTemplateCode(SmsTemplateCode.DISPATCH_NOTICE.getSMS_CODE());
            smsRequest.setPhoneNumber(task.getTel());

            SmsTemplateParam templateParam = new SmsTemplateParam();
            templateParam.setOrderCode(order.getOrderNo());
            templateParam.setName(userMapper.selectUserById(order.getPickupId()).getNickName());
            templateParam.setPhone(userMapper.selectUserById(order.getPickupId()).getPhonenumber());
            templateParam.setOptType(task.getType().equals("0")?"上门取件":"物流配送");

            smsRequest.setTemplateParam(templateParam);
            aliyunSmsService.AliyunSmsSend(smsRequest);

            /**
             * 上门取件通知
             * 0-物品 1-车辆
             */
            if(order.getBuss().equals("0")){
                String orderType = null;

                switch (task.getType()){
                    case "0":
                        orderType = "入库";
                        break;
                    case "1":
                        orderType = "出库";
                        break;
                    case "2":
                        orderType = "处置";
                        break;
                    case "3":
                        orderType = "调用";
                        break;

                }
                SmsRequest pSmsRequest = new SmsRequest();
                pSmsRequest.setTemplateCode(SmsTemplateCode.PICK_UP_NOTICE.getSMS_CODE());
                pSmsRequest.setPhoneNumber(task.getTel());

                SmsTemplateParam pTemplateParam = new SmsTemplateParam();
                pTemplateParam.setOrderType(orderType);
                pTemplateParam.setOrderCode(order.getOrderNo());
                pTemplateParam.setName(userMapper.selectUserById(order.getPickupId()).getNickName());
                pTemplateParam.setPhone(userMapper.selectUserById(order.getPickupId()).getPhonenumber());

                pSmsRequest.setTemplateParam(pTemplateParam);
                aliyunSmsService.AliyunSmsSend(pSmsRequest);
            }
        }
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WaybillVo updateWaybillByCarBo(WaybillBo bo) {
        WaybillVo vo = new WaybillVo();
        OrderVo order = orderMapper.selectOrderVoByID(bo.getOrderId());
        TaskVo task = taskMapper.selectTaskVoByID(order.getTaskId());
        Waybill update = BeanUtil.toBean(bo, Waybill.class);
//        validEntityBeforeSave(update);
//        boolean flag = baseMapper.updateById(update) > 0;
//        if(flag){
            BeanUtil.copyProperties(update,vo);

            /**
             * 上门取件通知
             */
            String orderType = null;

            switch (task.getType()){
                case "0":
                    orderType = "入库";
                    break;
                case "1":
                    orderType = "出库";
                    break;
                case "2":
                    orderType = "处置";
                    break;
                case "3":
                    orderType = "调用";
                    break;

            }
            SmsRequest pSmsRequest = new SmsRequest();
            pSmsRequest.setTemplateCode(SmsTemplateCode.PICK_UP_NOTICE.getSMS_CODE());
            pSmsRequest.setPhoneNumber(task.getTel());

            SmsTemplateParam pTemplateParam = new SmsTemplateParam();
            pTemplateParam.setOrderType(orderType);
            pTemplateParam.setOrderCode(order.getOrderNo());
            pTemplateParam.setName(userMapper.selectUserById(order.getPickupId()).getNickName());
            pTemplateParam.setPhone(userMapper.selectUserById(order.getPickupId()).getPhonenumber());

            pSmsRequest.setTemplateParam(pTemplateParam);
            aliyunSmsService.AliyunSmsSend(pSmsRequest);
//        }
        return vo;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(Waybill entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除运单
     *
     * @param ids 需要删除的运单主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean updateWithValidByIds(Collection<Waybill> waybills, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.updateBatchById(waybills);
    }

    public String getNo(){
        LambdaQueryWrapper<Waybill> lqw = Wrappers.lambdaQuery();
        Long maxNumObject = baseMapper.selectCount(lqw);
        String num = "";
        String todayStr = DateUtils.date2String(new Date(), "yyMMdd");

        if (maxNumObject == null) {
            num = todayStr + "000001";
        } else {
            String maxnumStr = "00000" + maxNumObject.toString();
            String maxnumNo = maxnumStr.substring(maxnumStr.length() - 6, maxnumStr.length());
            String nextnumNo = StringUtils.nextNo(maxnumNo, 6);
            num = todayStr + nextnumNo;
        }
        return num;
    }

    @Override
    public List<WaybillVo> waybillStatistics() {
        return baseMapper.waybillStatistics();
    }

    @Override
    public void updateWayBillByVo(WaybillVo vo) {
        Waybill update = BeanUtil.toBean(vo, Waybill.class);
        baseMapper.updateById(update);
    }

    @Override
    public String getSignByWaybill(String boxNo,String status) {
        List<WaybillVo> waybillVos = baseMapper.getSignByWaybill(boxNo,status);
        if(waybillVos != null && waybillVos.size() > 0){
            WaybillVo waybillVo = waybillVos.get(0);
            return waybillVo.getId();
        }
        return null;
    }
}
